package com.example.user1.config;

import com.example.user1.enums.ErrorEnum;
import com.example.user1.exception.BusinessException;
import com.example.user1.vo.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public Result<String> exceptionHandler(HttpServletRequest request, Exception e) {
        // 其余异常简单返回为服务器异常
        log.error("exception: ",e);
        return Result.failed(ErrorEnum.SERVER_ERROR);
    }

    @ExceptionHandler(value = BindException.class)
    public Result<String> exceptionHandler(HttpServletRequest request, BindException e) {
        // 其余异常简单返回为服务器异常
        log.error("bindException: ",e);
        return Result.failed(ErrorEnum.BIND_ERROR);
    }

    @ExceptionHandler(value = BusinessException.class)
    public Result<String> exceptionHandler(HttpServletRequest request, BusinessException e) {
        // 其余异常简单返回为服务器异常
        log.error("businessException: ",e);
        return Result.failed(e.getMessage());
    }
}